package com.atguigu.controller;

import com.atguigu.ssm.bean.User;
import com.atguigu.ssm.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * @author wk
 * @create 2021-06-08 11:15
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/test/{pageNum}")
    public String testList(Model model, @PathVariable("pageNum") int pageNum) {
        int pageSize = 4;
        PageInfo<User> pageInfo = userService.getUserList(pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        return "success";
    }


    @RequestMapping("/delete/{id}/{pageNum}")
    public String testDelete(@PathVariable("pageNum") int pageNum, @PathVariable("id") int id) {
        userService.deleteById(id);
        return "redirect:/test/" + pageNum;
    }

    @RequestMapping("/update/{id}/{pageNum}")
    public String testUpdate(Model model, @PathVariable("pageNum") int pageNum, @PathVariable("id") int id) {
        User user = userService.selectById(id);
        model.addAttribute("user", user);
        model.addAttribute("pageNum", pageNum);
        return "update";
    }
    @RequestMapping("/edit/{pageNum}")
    public String testEdit(User user,@PathVariable("pageNum") int pageNum){
        userService.update(user);
        return "redirect:/test/"+pageNum;
    }

}
